package com.GlobalPaint.app.ui.Home.friend;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.QunliaoActivityAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.HuoQuBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.activities.base.BaseApplication;
import com.jzy.message.db.ChatMsgDao;
import com.jzy.message.entity.xmpp.Msg;
import com.jzy.message.util.common.CommUtility;
import com.jzy.message.util.xmpp.Const;
import com.jzy.message.util.xmpp.PreferencesUtils;
import com.jzy.message.util.xmpp.XmppUtil;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class QunLiaoFragment extends Fragment {

    @BindView(R.id.xrecycler)
    XRecyclerView xrecycler;
    Unbinder unbinder;
    private LinearLayoutManager linearLayoutManager;
    private HuoQuBean bean;
    private List<List<HuoQuBean.DataBean>> list;
    private QunliaoActivityAdapter adapter;
    private String I,you1;//为了好区分，I就是自己，YOU就是对方
    public MultiUserChat muc1;
    private SimpleDateFormat sd;
    private ChatMsgDao msgDao;//读取数据库操作
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_qunliao, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        getQunLiao();
        initLoad();
        return view;
    }
    private void initView(View view) {
        linearLayoutManager=new LinearLayoutManager(getContext());
        xrecycler.setLayoutManager(linearLayoutManager);
        xrecycler.setLoadingMoreEnabled(false);
    }
    private void getQunLiao() {
        String url= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/list?userid="+ DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), HuoQuBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(HuoQuBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new QunliaoActivityAdapter(list,QunLiaoFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        list=new ArrayList<List<HuoQuBean.DataBean>>();
                        adapter=new QunliaoActivityAdapter(list, QunLiaoFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }
            }
        });
    }
    public  void deleteUser(final int userid,String Roomname,final int position) {
        String url= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/deleteByUserID?userid="+
                userid+"&roomname="+Roomname;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), HuoQuBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(HuoQuBean)o;
                if(bean.getStatus()==1){
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }
    private void initLoad() {
        xrecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/list?userid="+ DataManager.userEntity.getUserId();
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), HuoQuBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        HuoQuBean bean1=(HuoQuBean)o;
                        if( bean.getStatus() == 1){
                            if (bean.getData() != null && bean.getData().size()>0){
                                bean.getData().clear();
                                bean.getData().addAll(bean1.getData());
                                adapter.notifyDataSetChanged();
                                xrecycler.loadMoreComplete();
                            }
                        }
                        else {
                            Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                        }
                        xrecycler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        xrecycler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
            }
        });
    }
    public void job(){
        String url= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/list?userid="+ DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), HuoQuBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(HuoQuBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new QunliaoActivityAdapter(list, QunLiaoFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        list=new ArrayList<List<HuoQuBean.DataBean>>();
                        adapter=new QunliaoActivityAdapter(list, QunLiaoFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }

            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("qunlaio", "qunlaio: ");
        getQunLiao();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
